//
//  MovieViewController.m
//  Relax
//
//  Created by yanghuiyan on 16/4/18.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "MovieViewController.h"

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) YHYBaseTableView *movieTableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation MovieViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.dataSource = [NSMutableArray array];
    [self creatView];
    [self dataHandle];
}

#pragma mark - 数据解析
- (void)dataHandle {
    
    [YHYAFNetTool GETNetWirhUrl:@"http://v3.wufazhuce.com:8000/api/movie/list/0" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
//        NSLog(@"%@", result);
        for (NSDictionary *dic in result[@"data"]) {
            
            MovieModel *model = [[MovieModel alloc] initWithDataSource:dic];
            
            [self.dataSource addObject:model];
        }
        
        [self.movieTableView reloadData];
        
    } Failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - 创建视图

- (void)creatView {
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"电 影";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];

    // 创建TableView
    self.movieTableView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 114) style:UITableViewStylePlain];
    [_movieTableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movie"];
    _movieTableView.delegate = self;
    _movieTableView.dataSource = self;
    
    [self.view addSubview:_movieTableView];
    
}

/**
 *  tableView协议方法
 */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataSource.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 200*WIDTH/414;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movie"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.movieModel = self.dataSource[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc] init];
    
    MovieModel *model = self.dataSource[indexPath.row];
    
    detailVC.idNew = model.idNew;
    
    detailVC.titl = model.title;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
